package com.hospital.ecard.cardhandler;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.hospital.common.core.module.entity.ecard.BarcodeLogEntity;
import com.hospital.common.core.module.entity.ecard.ResponseEntity;
import com.hospital.common.core.utils.PinYinKit;
import com.hospital.common.core.utils.SpringContextHolder;
import com.hospital.ecard.module.entity.PatientInfo;
import com.hospital.ecard.module.service.PatientInfoService;
import com.hospital.ecard.patienthandler.PatientInfoHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lvyao
 * @description 身份证验证
 * @date 2020/7/7
 */
@Slf4j
public class IdentityCardHandler implements CardHandler {

    private PatientInfoService patientInfoService= SpringContextHolder.getBean(PatientInfoService.class);

    @Override
    public String dealCardInfo(BarcodeLogEntity barcodeLogEntity, HttpServletRequest request)  {
        ResponseEntity<PatientInfo> cardInfoResEntityResponseEntity=new ResponseEntity<>();
        PatientInfo patientInfo=null;
        if(barcodeLogEntity.getIdentityNo()!=null&&barcodeLogEntity.getIdentityNo().indexOf("|")>0){
            String[] arrList = barcodeLogEntity.getIdentityNo().split("|");
            if(arrList!=null&&arrList.length>6){
                String identityId=arrList[5];
                List<PatientInfo> patientInfoList = patientInfoService.selectPatientInfoByIdNo(identityId);
                if(patientInfoList!=null&&patientInfoList.size()>0){
                    patientInfo=patientInfoList.get(0);
                    PatientInfoHandler.updatePatientInfo(patientInfo,false);
                }else {
                    patientInfo=new PatientInfo();
                    patientInfo.setName(arrList[0]);
                    patientInfo.setSex(arrList[1]);
                    patientInfo.setIdNo(identityId);
                    patientInfo.setNation(arrList[2]);
                    patientInfo.setBirthPlace(identityId.substring(0,6));
                    String pinY= PinYinKit.getHanziInitials(arrList[0]);
                    patientInfo.setNamePhonetic(pinY.replace(':',' ').toUpperCase());
                    patientInfo.setMailingAddress(arrList[4]);
                    try {
                        patientInfo.setDateOfBirth(DateUtils.parseDate(arrList[3],"YYYY-MM-dd"));
                    }catch (Exception e){
                        log.error("出生日期转化错误",e);
                    }
                    PatientInfoHandler.updatePatientInfo(patientInfo,true);
                }
                cardInfoResEntityResponseEntity.setIsSuccess(10000);
                cardInfoResEntityResponseEntity.setBusinessInfo(patientInfo);
                return JSONObject.toJSONString(cardInfoResEntityResponseEntity, SerializerFeature.WriteMapNullValue);
            }
        }else if (barcodeLogEntity.getIdentityNo()!=null){
            List<PatientInfo> patientInfoList = patientInfoService.selectPatientInfoByIdNo(barcodeLogEntity.getIdentityNo());
            if (!CollectionUtils.isEmpty(patientInfoList)||patientInfoList.size()>0){
                cardInfoResEntityResponseEntity.setIsSuccess(10000);
                cardInfoResEntityResponseEntity.setBusinessInfo(patientInfoList.get(0));
                return JSONObject.toJSONString(cardInfoResEntityResponseEntity, SerializerFeature.WriteMapNullValue);
            }else {
                cardInfoResEntityResponseEntity.setErrMsg("未能获取该身份证号办理的信息");
                cardInfoResEntityResponseEntity.setIsSuccess(10010);
                return JSONObject.toJSONString(cardInfoResEntityResponseEntity,SerializerFeature.WriteMapNullValue);
            }
        }
        cardInfoResEntityResponseEntity.setErrMsg("无法获取正确的患者信息");
        cardInfoResEntityResponseEntity.setIsSuccess(10006);
        return JSONObject.toJSONString(cardInfoResEntityResponseEntity,SerializerFeature.WriteMapNullValue);
    }
}
